/*******************************************************************

    Author: Xiao
    Date: 2022/3/28 15:37

*******************************************************************/

package dao

import (
	"mcr/pkg/config"
	"mcr/pkg/utils"
	"regexp"
)

type User struct {
	UserId         int `gorm:"primarykey"`
	UserIcon       int
	UserSex        int
	UserAccount    string
	UserPassword   string
	UserSalt       string
	UserName       string
	UserHead       string
	UserRemark     string
	UserGroup      int
	UserIpreg      string
	UserOpenid     string
	UserExtra      string
	UserApp        int
	UserDevice     string
	UserTouchtip   string
	UserVip        string
	UserStatus     int
	UserCreatetime int64 `gorm:"autoCreateTime"`
	UserUpdatetime int64 `gorm:"autoUpdateTime"`
}

// Description: 通过密码进行登录
// Param: 用户ID或邮箱，密码
// return: 登录成功与否，是否禁用,用户ID，用户邮箱,帐号是否存在
func LoginByPassword(account, password string) (bool, bool, int, string, bool) {
	var u User
	ban := false
	sdb := MDB.Select("user_id", "user_account", "user_salt", "user_password")
	// 判断是邮箱还是Id
	pattern := `^[1-9][0-9]*$`
	reg := regexp.MustCompile(pattern)
	if reg.MatchString(account) {
		result := sdb.Where("user_id = ?", account).First(&u)
		if result.Error != nil {
			//if !errors.Is(result.Error, gorm.ErrRecordNotFound){
			//	logs.Log.Errorf("登录时查询数据库错误:%s",result.Error)
			//}
			return false, ban, 0, "", false
		}
		if utils.EncodeBySalt(password, u.UserSalt) == u.UserPassword {
			if u.UserStatus == 1 {
				ban = true
			}
			return true, ban, u.UserId, u.UserAccount, true
		}
	} else {
		result := sdb.Where("user_account = ?", account).First(&u)
		if result.Error != nil {
			//if !errors.Is(result.Error, gorm.ErrRecordNotFound){
			//	logs.Log.Errorf("登录时查询数据库错误:%s",result.Error)
			//}
			return false, ban, 0, "", false
		}
		if utils.EncodeBySalt(password, u.UserSalt) == u.UserPassword {
			if u.UserStatus == 1 {
				ban = true
			}
			return true, ban, u.UserId, u.UserAccount, true
		}
	}
	return false, ban, 0, "", true
}

// Description: 通过邮箱号查找用户ID，若没有找到则创建
// Param: 邮箱号
// return: 有无，是否被禁,ID
func GetUserIdByAccount(account, ip, device string) (bool, bool, int) {
	ban := false
	var u User
	salt := utils.GetVerifyEnglishCode(4)
	result := MDB.Where(User{UserAccount: account}).Attrs(User{
		UserSalt:     salt,
		UserPassword: utils.EncodeBySalt("123456", u.UserSalt),
		UserName:     account,
		UserHead:     config.ConfigValue.FrontendUrl + "new/images/nohead.jpg",
		UserGroup:    0,
		UserRemark:   utils.GetOneRemark(),
		UserIpreg:    ip,
		UserDevice:   device,
	}).FirstOrCreate(&u)
	if result.Error != nil {
		return false, ban, 0
	}
	if u.UserStatus == 1 {
		ban = true
	}
	return true, ban, u.UserId
}

// 根据用户id获取用户状态
func GetUserStatusById(id int) (int, error) {
	var u User
	result := MDB.Where("user_id = ?", id).Select("user_status").First(&u)
	if result.Error != nil {
		//if !errors.Is(result.Error, gorm.ErrRecordNotFound){
		//	logs.Log.Errorf("查询用户状态错误:%s",result.Error)
		//}
		return 0, result.Error
	}
	return u.UserStatus, nil
}

// 更新用户信息
func UpdateUserInfo(uid int, column map[string]interface{}) error {
	result := MDB.Model(&User{}).Where("user_id = ?", uid).Updates(column)
	return result.Error
}

// 更新用户设备
func UpdateUserDevice(uid int, device string) error {
	result := MDB.Model(&User{}).Where("user_id = ?", uid).Update("user_device", device)
	return result.Error
}

// 通过用户id获取用户信息
func GetUserByUserId(uid int) (User, error) {
	var u User
	//result := MDB.Select("user_id","user_sex","user_device","user_name","user_head","user_remark").Where("user_id = ?",uid).First(&u)
	result := MDB.Omit("user_password", "user_salt", "user_ipreg", "user_openid", "user_app", "user_createtime", "user_updatetime").Where("user_id = ?", uid).First(&u)
	return u, result.Error
}
